var e = require("../../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../../@babel/runtime/helpers/slicedToArray"), a = require("../../../../@babel/runtime/helpers/createForOfIteratorHelper"), r = e(require("../../../../api/api_queryRemain.js")), n = e(require("../../../../api/wxRequest")), i = require("../../../../api/common"), o = e(require("@vant/weapp/dialog/dialog")), c = e(require("../../../../behaviors/auth")), s = e(require("../../../../utils/Tips")), u = require("../../../../utils/util"), d = require("../../../../api/common.js"), l = {
    district: /\S/,
    detailedAddress: /^(?=(.*[\u4e00-\u9fa5].*){4})/g,
    customer: /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/,
    customerID: /^[1-9]\d{5}(18|19|2([0-9]))\d{2}(0[0-9]|10|11|12)([0-2][1-9]|[1-2][0-9]|30|31)\d{3}[0-9Xx]$/,
    phone: /^1[3-9]\d{9}$/
};

Component({
    behaviors: [ c.default ],
    attached: function() {
        this.init();
    },
    properties: {
        chargePhone: {
            type: String,
            value: ""
        }
    },
    data: {
        discounts: !0,
        selectAmount: "",
        amounts: [ {
            value: 30,
            recommend: !1,
            subContent: ""
        }, {
            value: 50,
            recommend: !1,
            subContent: ""
        }, {
            value: 100,
            recommend: !1,
            subContent: ""
        }, {
            value: 300,
            recommend: !1,
            subContent: ""
        } ],
        show: {
            areaPicker: !1
        },
        userInfo: {
            phone: "",
            customer: "",
            customerID: "",
            provinceName: "",
            provinceCode: "",
            cityName: "",
            cityCode: "",
            districtName: "",
            districtCode: "",
            detailedAddress: "",
            enableOrderNetwork: void 0
        },
        genArea: [],
        areaColumnsDicts: {
            province_city: {},
            city_region: {}
        },
        areaColumns: [ {
            values: [],
            className: "column1"
        }, {
            values: [],
            className: "column2"
        }, {
            values: [],
            className: "column3"
        } ],
        testInfoList: {
            district: {
                source: [ "userInfo.provinceName", "userInfo.cityName", "userInfo.districtName" ],
                errorType: !1,
                errorMsg: "请选择所在地区"
            },
            detailedAddress: {
                source: "userInfo.detailedAddress",
                errorType: !1,
                errorMsg: "请输入详细地址"
            },
            customer: {
                source: "userInfo.customer",
                errorType: !1,
                errorMsg: "请输入正确姓名"
            },
            customerID: {
                source: "userInfo.customerID",
                errorType: !1,
                errorMsg: "请输入正确身份证号"
            },
            phone: {
                source: "userInfo.phone",
                errorType: !1,
                errorMsg: "请输入正确手机号"
            }
        }
    },
    methods: {
        gotoRule: function() {
            this.openWebview("https://txwk.10010.com/wkpen/view/d0f0A5D33c.html");
        },
        openWebview: function(e) {
            wx.navigateTo({
                url: "/pages/webview/index?url=" + encodeURIComponent(e)
            });
        },
        init: function() {
            this.init_area();
        },
        init_area: function() {
            var e = this;
            (0, d.getArea)().then(function(t) {
                var a = e.districtAreaprovinceGen(t);
                e.setData({
                    genArea: a
                }), e.init_areaPicker(a), e.tidy_areaPickerColumn(a);
            });
        },
        init_areaPicker: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
            if (e) {
                var t = e.map(function(e) {
                    return e.text;
                }), a = e[0].children, r = e[0].children[0].children, n = a.map(function(e) {
                    return e.text;
                }), i = r.map(function(e) {
                    return e.text;
                });
                this.setData({
                    areaColumns: [ {
                        values: t,
                        className: "column1"
                    }, {
                        values: n,
                        className: "column2"
                    }, {
                        values: i,
                        className: "column3"
                    } ]
                });
            } else console.warn("无法初始展示省市区");
        },
        tidy_areaPickerColumn: function() {
            var e, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], r = {}, n = {}, i = a(t);
            try {
                for (i.s(); !(e = i.n()).done; ) {
                    var o = e.value, c = o.text, s = o.children, u = void 0 === s ? [] : s;
                    r[c] || (r[c] = []);
                    var d, l = a(u);
                    try {
                        for (l.s(); !(d = l.n()).done; ) {
                            var f = d.value, h = f.text, m = f.children, p = void 0 === m ? [] : m;
                            r[c].push(h), n[h] = [];
                            var v, g = a(p);
                            try {
                                for (g.s(); !(v = g.n()).done; ) {
                                    var C = v.value, I = C.text;
                                    n[h].push(I);
                                }
                            } catch (e) {
                                e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
                                g.e(e);
                            } finally {
                                g.f();
                            }
                        }
                    } catch (e) {
                        e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
                        l.e(e);
                    } finally {
                        l.f();
                    }
                }
            } catch (e) {
                e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
                i.e(e);
            } finally {
                i.f();
            }
            this.setData({
                areaColumnsDicts: {
                    province_city: r,
                    city_region: n
                }
            });
        },
        districtAreaprovinceGen: function(e) {
            var t = [], a = [], r = [];
            return e.forEach(function(e) {
                e.ESS_CITY_CODE ? e.DISTRICT_CODE ? r.push({
                    text: e.DISTRICT_NAME,
                    originalData: e
                }) : a.push({
                    text: e.CITY_NAME,
                    originalData: e
                }) : t.push({
                    text: e.PROVINCE_NAME,
                    originalData: e
                });
            }), r.forEach(function(e) {
                var t = a.findIndex(function(t) {
                    return t.originalData.ESS_CITY_CODE === e.originalData.ESS_CITY_CODE;
                });
                a[t].children || (a[t].children = []), a[t].children.push(e);
            }), a.forEach(function(e) {
                var a = t.findIndex(function(t) {
                    return t.originalData.ESS_PROVINCE_CODE === e.originalData.ESS_PROVINCE_CODE;
                });
                t[a].children || (t[a].children = []), t[a].children.push(e);
            }), t;
        },
        showAreaCmp: function() {
            this.setData({
                show: {
                    areaPicker: !0
                }
            });
        },
        formInput: function(e) {
            var t = this.data.userInfo, a = e.target.dataset.info, r = e.detail.value;
            t[a] = r, this.setData({
                userInfo: t
            });
        },
        discountTick: function() {
            var e = this.data.discounts;
            this.setData({
                discounts: !e
            });
        },
        selectAmount: function(e) {
            var t = e.currentTarget.dataset.amount;
            this.setData({
                selectAmount: t
            });
        },
        payFor: function() {
            var e = this.data, t = e.discounts, a = e.selectAmount, r = e.chargePhone;
            if (a) if ((0, u.testPhone)(r)) if (t) {
                if (this.infoTest()) {
                    var n = this.data.userInfo;
                    this.chargeAmount_broadband({
                        phone: r,
                        amount: a,
                        broadbandInfo: n
                    });
                }
            } else this.chargeAmount_normal(r, a); else wx.showToast({
                icon: "none",
                title: "未获取到正确充值的手机号~！",
                mask: !0
            }); else wx.showToast({
                icon: "none",
                title: "请选择充值金额~！",
                mask: !0
            });
        },
        chargeAmount_broadband: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, a = t.phone, r = t.amount, n = t.broadbandInfo;
            s.default.loading();
            var c = 100 * r;
            (0, i.chargeBroadband)({
                amountFee: c,
                phone: a,
                otherInfo: {
                    certName: n.customer,
                    certNo: n.customerID,
                    contractPhone: n.phone,
                    provinceCode: n.provinceCode,
                    cityCode: n.cityCode,
                    districtCode: n.districtCode,
                    address: n.detailedAddress,
                    proId: "1123179121001009248",
                    channel: "01-0219-4599-b2aa"
                }
            }).then(function(t) {
                s.default.loaded(), "0" === t.code ? t.data.pkg ? e.wxPay(t.data) : o.default.alert({
                    context: e,
                    message: "调起支付失败，请稍候重试"
                }) : "10002" === t.code ? o.default.confirm({
                    context: e,
                    title: "温馨提示",
                    message: "您未绑定王卡助手，请先绑定",
                    confirmButtonOpenType: "getPhoneNumber",
                    confirmButtonText: "立即绑定"
                }) : "0002" === t.code ? o.default.alert({
                    context: e,
                    title: "温馨提示",
                    message: "当前号码非特邀用户，无法参与本活动"
                }).then(function() {
                    wx.redirectTo({
                        url: "/pages/charge/index"
                    });
                }) : o.default.alert({
                    context: e,
                    message: t.message
                });
            }).catch(function() {
                s.default.loaded();
            });
        },
        chargeAmount_normal: function(e, t) {
            var a = this;
            s.default.loading();
            var n = 100 * t;
            r.default.charge({
                phone: e,
                amountFee: n
            }).then(function(e) {
                s.default.loaded(), "0" === e.code ? e.data.pkg ? a.wxPay(e.data) : o.default.alert({
                    context: a,
                    message: "调起支付失败，请稍候重试"
                }) : "10002" === e.code ? o.default.confirm({
                    context: a,
                    title: "温馨提示",
                    message: "您未绑定王卡助手，请先绑定",
                    confirmButtonOpenType: "getPhoneNumber",
                    confirmButtonText: "立即绑定"
                }) : o.default.alert({
                    context: a,
                    message: e.message
                });
            }).catch(function() {
                s.default.loaded();
            });
        },
        wxPay: function(e) {
            n.default.wxPay(e).then(function(e) {
                var t = e.appid, a = e.path;
                t && "wxc47d9eb33eec8b6a" != t && a ? wx.openEmbeddedMiniProgram({
                    appId: t,
                    path: a,
                    success: function() {
                        console.log("成功打开小程序appid：", t, ";path：", a);
                    },
                    fail: function(e) {
                        console.log(e);
                    }
                }) : a ? wx.navigateTo({
                    url: a
                }) : console.log("无可跳转地址");
            });
        },
        infoTest: function() {
            var e = this, t = !0, r = this.data.testInfoList, n = function() {
                if (Object.hasOwnProperty.call(r, i)) {
                    var n = r[i].source;
                    if (n instanceof Array) {
                        var o, c = a(n);
                        try {
                            var s = function() {
                                var a = o.value.split("."), n = e.data;
                                if (a.forEach(function(e) {
                                    n = n[e];
                                }), !l[i].test(n)) return r[i].errorType = !0, e.setData({
                                    testInfoList: r
                                }), t = !1, 1;
                                r[i].errorType = !1, e.setData({
                                    testInfoList: r
                                });
                            };
                            for (c.s(); !(o = c.n()).done && !s(); ) ;
                        } catch (e) {
                            e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
                            c.e(e);
                        } finally {
                            c.f();
                        }
                        if (!t) return 0;
                    } else {
                        var u = n.split("."), d = e.data;
                        if (u.forEach(function(e) {
                            d = d[e];
                        }), !l[i].test(d)) return r[i].errorType = !0, e.setData({
                            testInfoList: r
                        }), t = !1, 0;
                        r[i].errorType = !1, e.setData({
                            testInfoList: r
                        });
                    }
                }
            };
            for (var i in r) if (0 === n()) break;
            return t;
        },
        areaPicker_confirm: function(e) {
            var a = e.detail.value, r = this.data.genArea, n = t(a, 3), i = n[0], o = n[1], c = n[2], s = r.findIndex(function(e) {
                var t = e.text;
                return i === t;
            }), u = r[s], d = u.originalData, l = d.PROVINCE_NAME, f = d.ESS_PROVINCE_CODE, h = u.children, m = h.findIndex(function(e) {
                var t = e.text;
                return o === t;
            }), p = h[m], v = p.originalData, g = v.CITY_NAME, C = v.ESS_CITY_CODE, I = p.children, y = I.findIndex(function(e) {
                var t = e.text;
                return c === t;
            }), D = I[y].originalData, x = D.DISTRICT_NAME, _ = D.DISTRICT_CODE;
            this.setData({
                userInfo: {
                    provinceName: l,
                    provinceCode: f,
                    cityName: g,
                    cityCode: C,
                    districtName: x,
                    districtCode: _
                },
                show: {
                    areaPicker: !1
                }
            });
        },
        areaPicker_cancel: function() {
            this.setData({
                show: {
                    areaPicker: !1
                }
            });
        },
        areaPicker_change: function(e) {
            var t = e.detail, a = t.index, r = t.value, n = t.picker;
            switch (a) {
              case 0:
                this.areaPicker_render({
                    locationVal: r,
                    changeIndex: a,
                    changeType: "province",
                    pickerCmp: n
                });
                break;

              case 1:
                this.areaPicker_render({
                    locationVal: r,
                    changeIndex: a,
                    changeType: "city",
                    pickerCmp: n
                });
            }
        },
        areaPicker_render: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = e.locationVal, a = e.changeIndex, r = e.changeType, n = e.pickerCmp;
            if ("province" == r) {
                var i = this.data.areaColumnsDicts.province_city, o = this.data.areaColumnsDicts.city_region, c = t[a], s = i[c], u = s[0], d = o[u];
                n.setColumnValues(a + 1, s), n.setColumnValues(a + 2, d);
            } else if ("city" == r) {
                var l = this.data.areaColumnsDicts.city_region, f = t[a], h = l[f];
                n.setColumnValues(a + 1, h);
            }
            getApp().picker = picker;
        }
    }
});